define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/order/index' + location.search,
                    add_url: 'shop/order/add',
                    edit_url: 'shop/order/edit',
                    // del_url: 'shop/order/del',
                    // multi_url: 'shop/order/multi',
                    // import_url: 'shop/order/import',
                    table: 'shop_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'acct_id', title: __('平台'), visible: Config.acct_visible, searchList: Config.adminlist, formatter: Table.api.formatter.normal },
                        { field: 'order_no', title: __('Order_no'), operate: 'LIKE' },
                        { field: 'custom.new_name', title: __('Custom.name'), operate: false },
                        { field: 'custom.name', title: __('Custom.name'), visible: false, operate: 'LIKE' },
                        { field: 'user.new_nickname', title: __('下单用户'), operate: false },
                        { field: 'user.nickname', title: __('下单用户'), visible: false, operate: 'LIKE' },
                        // {field: 'user_id', title: __('User_id')},
                        // {field: 'custom_id', title: __('Custom_id')},
                        { field: 'status', title: __('Status'), searchList: { "-0.5": __('Status -0.5'), "-0.4": __('Status -0.4'), "0": __('Status 0'), "0.1": __('Status 0.1'), "0.5": __('Status 0.5'), "0.6": __('Status 0.6'), "1": __('Status 1'), "99": __('Status 99'), '-1': __('审核失败') }, formatter: Table.api.formatter.status },
                        { field: 'source_type', title: __('来源'), searchList: { "0": __('普通单'), "1": __('手工单') }, formatter: Table.api.formatter.status },
                        { field: 'send_type', title: __('发货方'), searchList: { "0": __('无'), "1": __('总平台'), "2": __('子平台') }, formatter: Table.api.formatter.label },

                        // {field: 'total', title: __('Total')},
                        { field: 'total_fee', title: __('Total_fee'), operate: false },
                        { field: 'pay_fee', title: __('Pay_fee'), operate: false },
                        { field: 'send_fee', title: __('运费'), operate: false },
                        { field: 'original_price', title: __('成本价'), operate: false },
                        {
                            field: 'lr', title: __('利润'), formatter: function (value, row) {
                                return (Number(row.total_fee) - Number(row.original_price)).toFixed(2);
                            }
                        },
                        { field: 'pay_time', title: __('Pay_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        // {field: 'finished_time', title: __('Finished_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        { field: 'consign_name', title: __('Consign_name'), operate: 'LIKE' },
                        { field: 'consign_mobile', title: __('Consign_mobile'), operate: 'LIKE' },
                        { field: 'consign_address', title: __('Consign_address'), operate: 'LIKE' },
                        { field: 'total', title: __('订单总数量'), operate: 'LIKE' },
                        { field: 'total_weight', title: __('订单总重量'), operate: 'LIKE' },
                        // {field: 'pay_image', title: __('Pay_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        { field: 'express_id', title: __('Express_com'), searchList: Config.expresslist, formatter: Table.api.formatter.normal },
                        { field: 'express_no', title: __('Express_no'), operate: 'LIKE' },
                        { field: 'distribution_id', title: __('配送方式'), searchList: Config.distributionlist, formatter: Table.api.formatter.normal },
                        {
                            field: 'ordergoods', title: __('商品信息'), operate: false, formatter: function (row, col) {
                                let str = "";
                                for (let i = 0; i < row.length; i++) {
                                    str += (row[i].goods_info_json.title + "*" + row[i].total + (row[i].goods_info_json.unit || '斤') + ' ' + row[i].pay_price + '元<br>');
                                }
                                return str;
                            }
                        },

                        { field: 'remark', title: __('Remark'), operate: 'LIKE' },
                        { field: 'jdy_id', title: __('金蝶ID'), operate: 'LIKE' },


                        // {field: 'express_com', title: __('Express_com'), searchList: {"SF":__('Express_com sf'),"HTKY":__('Express_com htky'),"ZTO":__('Express_com zto'),"STO":__('Express_com sto'),"YTO":__('Express_com yto'),"YD":__('Express_com yd'),"YZPY":__('Express_com yzpy'),"EMS":__('Express_com ems'),"JD":__('Express_com jd'),"UC":__('Express_com uc'),"DBL":__('Express_com dbl'),"JTSD":__('Express_com jtsd'),"ZYE":__('Express_com zye'),"ZJS":__('Express_com zjs')}, formatter: Table.api.formatter.normal},
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },

                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                // {
                                //     name: 'bargain',
                                //     title: '议价',
                                //     text: '议价',
                                //     classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                //     url: 'shop/order/bargain',//接口地址 控制器名/方法名
                                //     visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                //         let options = table.bootstrapTable('getOptions');
                                //         if (options.operateBargain) {
                                //             if (row.status == -0.4) {
                                //                 return true;//显示
                                //             } else {
                                //                 return false;//显示
                                //             }
                                //         }
                                //     },
                                // },
                                {
                                    name: 'examine',
                                    title: '审核',
                                    text: '审核',
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    url: 'shop/order/examine',//接口地址 控制器名/方法名
                                    visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        let options = table.bootstrapTable('getOptions');
                                        if (options.operateExamine) {
                                            if (row.status <= 0) {
                                                return true;//显示
                                            } else {
                                                return false;//显示
                                            }
                                        }
                                    },
                                },
                                {
                                    name: 'transit',
                                    title: '运输',
                                    text: '运输',
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    url: 'shop/order/transit',//接口地址 控制器名/方法名
                                    visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        let options = table.bootstrapTable('getOptions');
                                        if (options.operateTransit) {
                                            if (row.status >= 0.6) {
                                                return true;//显示
                                            } else {
                                                return false;//显示
                                            }
                                        }
                                    },
                                },
                                {
                                    name: 'operation',
                                    title: '操作',
                                    text: '操作',
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    url: 'shop/order/operation',//接口地址 控制器名/方法名
                                    visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        let options = table.bootstrapTable('getOptions');
                                        if (options.operateOperation) {
                                            if (row.status > 0) {
                                                return true;//显示
                                            } else {
                                                return false;//不显示
                                            }
                                        }

                                    },
                                },
                                {
                                    name: 'query',
                                    title: '物流',
                                    text: '物流',
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    url: 'shop/order/query?express_id={express_id}&express_no={express_no}',//接口地址 控制器名/方法名
                                    visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if (row.express_no) {
                                            return true;//显示
                                        } else {
                                            return false;//显示
                                        }

                                    },
                                },
                                {
                                    name: 'detail',
                                    title: '详情',
                                    text: '详情',
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    url: 'shop/order/detail',//接口地址 控制器名/方法名
                                    visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        let options = table.bootstrapTable('getOptions');

                                        if (options.operateDetail) {
                                            return true;//显示
                                        } else {
                                            return false;//显示
                                        }

                                    },
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'shop/order/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'shop/order/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'shop/order/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        bargain: function () {
            Controller.api.bindevent();
        },
        transit: function () {
            Controller.api.bindevent();
        },
        operation: function () {
            Controller.api.bindevent();
        },
        examine: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
        },

        add: function () {
            $(document).on("fa.event.appendfieldlist", "#options-form .btn-append", function (e, obj) {
                //绑定动态下拉组件
                Form.events.selectpage(obj);
                //绑定日期组件
                //   Form.events.datetimepicker(obj);
                //绑定上传组件
                // Form.events.faupload(obj);

                // //上传成功回调事件，变更按钮的背景
                // $(".upload-image", obj).data("upload-success", function (data) {
                //     $(this).css("background-image", "url('" + Fast.api.cdnurl(data.url) + "')");
                // })
            });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
